<template>
	<view class="goodWrapper">
		<view class="goodContainer">
			<view class="searchInput">
				<view class="goodFilter">
					<uniIcon type="search" size="14" style="height:30px;line-height: 30px;margin-left: 5px;margin-right: 5px;color: #a7aab3;" /><input
					 placeholder="输入商品名称/商品编号查询" class="searchOption" v-model="inputStr"></input>
					<button class="searchBt" @tap="suosuo()">查询</button>
				</view>
				<view class="searchOptions">
					<button class="getPicture" @tap="tel()"></button>
				</view>
			</view>
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
			 @scroll="scroll">
				<checkbox-group>
					<view class="goodsItem" v-for="(item,index) in GoodList" :key="index" @tap="navigatorTo()">
						<view class="checkboxContainer" v-if="isSelectMore==1">
							<view :class="['checkboxTemp',{'checkboxTempActive':checkGoodList[index].checked}]" @tap="selectNowGood(index)"></view>
						</view>
						<image :src="item.Goods_Image_Path1" class="goodsImg" mode="aspectFill" @tap="selectGoods(index)"></image>
						<view class="goodInfo">
							<view class="goodName">
								<text @tap="selectGoods(index)"> {{item.GoodsName}}</text>
								<button @tap="lookGoodInfo(index)">{{item.isSet?"已设置":"修改参数"}}</button>
							</view>
							<view class="priceWrap">
								<view class="price">
									<text>编号:{{item.GoodsCode}}</text>
								</view>
								<view class="price">
									<view><text>单价:￥{{item.Price | sendPrice}}</text>
									</view>
									<view><text>数量:</text>
										<input type="number" v-model="item.Qty"  />
									</view>
								</view>
								<view class="price goodSet">
									<view><text>库存:{{item.stockqty}}</text></view>
								</view>
							</view>
						</view>
					</view>
				</checkbox-group>
			</scroll-view>
		</view>
		<view class="goodsCount">
			<view @tap="addCheckGoodList(0)" :class="{'hasSelect':hasSelect}" v-if="isSelectMore==1">添加已选</view>
			<view @tap="addCheckGoodList(1)" v-if="isSelectMore==1">添加全部</view>
			<view v-if="isSelectMore==0">商品总数:{{GoodList.length}}</view>
			<!-- <view>删除已选</view> -->
		</view>
		<view class="cu-modal" :class="modalName=='stockqtyModal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">以下商品库存不足</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					<view class="StoreListWrap">
						<view v-for="(item,index) in stockqtyList" :key="index">{{item.GoodsName}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import allConst from "@/Json.js"
	import uniIcon from '@/components/uni-icon/uni-icon.vue';
	import * as Apis from '@/syApis/syAddOrder/syaddApis.js';
	export default {
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				GoodList: [],
				isShowGoodsType: 0,
				typeName: "全部分类",
				StoreID: 0,
				isShowCar: false,
				// 多选商品复选框
				checkGoodList: [],
				//已选商品列表
				selectGoodList: [],
				//搜索框的条件
				inputStr: "",
				IsCx: false,
				BillName: "",
				hasSelect: false,
				unLoadimg: '../../static/unUplad.png',
				//是否多选商品
				isSelectMore: 0,
				BillType: 0,
				isNumQtStorm: false,
				stockqtyList: [],
				modalName:null,
				ClientID:"",
				TipModel:""
			}
		},
		created() {

		},
		onShow() {
			this.$eventHub.$off('fire');
		},
		onLoad(options) {
			uni.showLoading({
				title: "加载中...",
					mask:false
			})
			this.StoreID = options.StoreID;
			this.IsCx = options.IsCx;
			this.BillType = options.IsCx;
			this.BillName = options.BillName;
			this.isSelectMore = options.isSelectMore;
			this.ClientID = options.ClientID || "";
			this.getGoodList();
			var that = this;
			let SystemSettingInfo = uni.getStorageSync("SystemSettingInfo", ""); //存储系统设置
			SystemSettingInfo = JSON.parse(SystemSettingInfo);
			SystemSettingInfo.forEach((item, index) => {
				if (item.SystemName == '是否允许负库存出库') {
					if (item.Value == "1") {
						that.isNumQtStorm = true;
					}
				}
			});
		},
		components: {
			uniIcon
		},
		filters: {
			sendPrice(val) {
				if (val == "" || typeof val == 'undefined') {
					return 0;
				}
				return Math.round(val * 100) / 100;
			}
		},
		methods: {
			hideModal(){
				this.modalName = null;
			},
			//搜索
			suosuo() {
				console.log("进行了搜索");
				uni.showLoading({
					title: "加载中...",
					mask:false
				})
				let data = {};
				if (this.inputStr == '') {
					data = {
						StoreID: this.StoreID,
						BillName: this.BillName
						// Filter:"GoodsName='"+this.inputStr+"'"
					};
				} else {
					data = {
						StoreID: this.StoreID,
						Filter: "GoodsName like '%" + this.inputStr + "%' or GoodsCode like '%" + this.inputStr + "%'",
						BillName: this.BillName,
						BarCode:"",
						Model:"like"
					};
				}
				console.log("条件是" + JSON.stringify(data));
				this.getGoodList(data);
			},
			AddListen() {
				var _this = this;
				this.$eventHub.$on('fire', function(data) {
					let GoodsItem = JSON.parse(data.NewGoodInfo);
					
					_this.pullSelectGoodInfo(GoodsItem, data.index);
					_this.$eventHub.$off('fire');
				});
			},
			//添加已选 type 0 添加已选 1添加全部
			addCheckGoodList(type) {
				var _this = this;
				_this.selectGoodList = [];
				_this.stockqtyList = [];
				var count = 0;
				this.checkGoodList.forEach((item, index) => {
					if (type == 0) {
						if (item.checked) {
							count++;
							if (_this.GoodList[index].Qty > _this.GoodList[index].stockqty && !_this.isNumQtStorm && _this.BillType==4) {
								_this.stockqtyList.push(_this.GoodList[index]);
							}
							_this.selectGoodList.push(_this.GoodList[index]);
						}
					} else {
						count++;
						if (_this.GoodList[index].Qty > _this.GoodList[index].stockqty && !_this.isNumQtStorm && _this.BillType==4) {
							_this.stockqtyList.push(_this.GoodList[index]);
						}
						_this.selectGoodList.push(_this.GoodList[index]);
						// _this.selectGoodList.push(_this.GoodList[index]);
					}
				});
				if(_this.stockqtyList.length>0){
					  this.modalName = "stockqtyModal";
			          return;
				}
				if (count == 0) {
					uni.showToast({
						title: "请至少选择一件商品",
						duration: 1000,
						icon: "none"
					});
					return;
				}
				// console.log(count);
				this.$eventHub.$emit('fires', JSON.stringify(_this.selectGoodList));
				uni.navigateBack({
					delta: 1
				})
				////console.tablele(_this.selectGoodList);
			},
			//添加商品
			selectGoods(index) {
				var _this = this;
				//如果开启了库存不足提醒且订单为销售订单
				if (_this.GoodList[index].Qty > _this.GoodList[index].stockqty && !this.isNumQtStorm && this.BillType==4) {
					Apis.showTip("当前库存不足");
					return;
				}
				_this.selectGoodList = [];
				var count = 0;
				_this.selectGoodList.push(_this.GoodList[index]);
				this.$eventHub.$emit('fires', JSON.stringify(_this.selectGoodList));
				uni.navigateBack({
					delta: 1
				})
			},
			//查看商品详情
			lookGoodInfo(index) {
				var goodData = JSON.stringify(this.GoodList[index]);
				uni.setStorageSync("ChangeGoodData",goodData);
				this.AddListen();
				console.log(this.BillName);
				uni.navigateTo({
					url: "./goodInfoDetail?StoreID=" + this.StoreID + "&selectIndex=" + index + "&IsCx=" +
						this.IsCx +
						"&BillName=" + this.BillName+"&ClientID="+this.ClientID
				})
			},
			//商品设置修改的数据
			pullSelectGoodInfo(newGoodInfo, index) {
				
				
				// this.GoodList[index].Qty = newGoodInfo.Qty;
				// this.GoodList[index].GoodsStorm = newGoodInfo.stockqty;
				// this.GoodList[index].StoreID = newGoodInfo.StoreID;
				// this.GoodList[index].Tax = newGoodInfo.Tax;
				// this.GoodList[index].Discount = newGoodInfo.Discount;
				// this.GoodList[index].Remark = newGoodInfo.Remark;
				// this.GoodList[index].startTime = newGoodInfo.startTime;
				// this.GoodList[index].EndTime = newGoodInfo.EndTime;
				// this.GoodList[index].Price = newGoodInfo.Price;
				// this.GoodList[index].UnitName = newGoodInfo.UnitName;
				// this.GoodList[index].urate = newGoodInfo.urate;
				// this.GoodList[index].UnitID = newGoodInfo.UnitID;
				// this.GoodList[index].Amount = newGoodInfo.Amount;
				// this.GoodList[index].DiscountPrice = newGoodInfo.DiscountPrice;
				// this.GoodList[index].stockqty = newGoodInfo.stockqty;
				this.GoodList[index] = newGoodInfo;
				this.GoodList[index].isSet = true;
				let GoodList = Object.assign([],this.GoodList);
				console.log(GoodList);
				this.GoodList = GoodList;
				console.log(this.GoodList);
			},
			//复选框选择
			selectNowGood(index) {
				if (this.checkGoodList[index].checked) {
					this.checkGoodList[index].checked = false;
				} else {
					this.checkGoodList[index].checked = true;
				}
				var hasSelect = 0;
				this.checkGoodList.forEach((item, index) => {
					if (item.checked) {
						hasSelect++;
					}
				});
				if (hasSelect > 0) {
					this.hasSelect = true;
				} else {
					this.hasSelect = false;
				}
			},
			DiscountPrice(item) {
				return parseFloat(item.Price) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
					item.Tax) / 100);
			},
			//计算总价
			Amount(item) {
				return parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
					item.Tax) / 100);
				// return  PFPrice;
			},
			//查询商品
			getGoodList(filter) {
				if (filter == undefined) {
					filter = {
						StoreID: this.StoreID,
						BillName: this.BillName
					}
				}
				console.log(filter);
				var that = this;
				var tempcheckGoodList = [];
				console.log(that.unLoadimg);
				Apis.GetGoodsMessage(filter, function(res) {
					uni.hideLoading();
					if(res.state){
						if (res.data.ErrorMsg == "获取登录信息失败") {
						
						} else if (res.data.ErrorMsg != "") {
							console.log(res.data.ErrorMsg);
						
						} else {
							console.log(res);
							var item = {};
							if (res.data.ResultMsg.length == 0) {
								Apis.showTip("查无数据");
							}
						
							res.data.ResultMsg.forEach((item, index) => {
								item = {
									checked: false
								};
								tempcheckGoodList.push(item);
								res.data.ResultMsg[index].Qty = 1;
								res.data.ResultMsg[index].isSet = false;
								res.data.ResultMsg[index].Discount = 100;
								res.data.ResultMsg[index].Tax = 0;
								res.data.ResultMsg[index].IsGift = 0;
								res.data.ResultMsg[index].Remark = "";
								res.data.ResultMsg[index].GoodsStorm = 0;
								// console.log(that.BillType,res.data.ResultMsg[index].SalePrice);
								if (that.BillType == 4 || that.BillType == 13) {
						
									res.data.ResultMsg[index].Price = Math.round(res.data.ResultMsg[index].SalePrice * 100) / 100;
								} else {
						
									res.data.ResultMsg[index].Price = Math.round(res.data.ResultMsg[index].InPrice * 100) / 100;
								}
								res.data.ResultMsg[index].barcode = res.data.ResultMsg[index].BarCode;
								res.data.ResultMsg[index].urate = res.data.ResultMsg[index].urate;
								res.data.ResultMsg[index].StoreID = that.StoreID;
								res.data.ResultMsg[index].isChange = false;
								res.data.ResultMsg[index].stockqty = Math.round(res.data.ResultMsg[index].stockqty * 100) / 100;
								// res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
								// res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
								// res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
								res.data.ResultMsg[index].DiscountPrice = that.DiscountPrice(res.data.ResultMsg[index]);
								res.data.ResultMsg[index].Amount = that.Amount(res.data.ResultMsg[index]);
								if (res.data.ResultMsg[index].Goods_Image_Path1 == '') {
									res.data.ResultMsg[index].Goods_Image_Path1 = that.unLoadimg;
								} else {
									res.data.ResultMsg[index].Goods_Image_Path1 = allConst.pictureUrl + res.data.ResultMsg[index].Goods_Image_Path1;
								}
							});
							that.checkGoodList = tempcheckGoodList;
							that.GoodList = res.data.ResultMsg;
							that.inputStr = "";
						}
					}else{
						Apis.showTip("服务器开小差了");
					}				
				});
			},
			back: function() {
				uni.navigateBack({
					delta: 1
				})
			},
			//关闭购物车
			cancelCar: function() {
				this.isShowCar = false
			},
			//开启购物车
			openCar: function() {
				this.isShowCar = !this.isShowCar
			},
			getSm: function() {
				uni.scanCode({
					success: function(res) {

					}
				});

			},
			choosGoods(index) {

			},
			navigatorTo(e) {
				if (this.type == 0) {
					uni.navigateTo({
						url: '../detail/detail',
					});
				}
			},
			setTypeName: function(msg) {
				//获取点击对象      
				this.typeName = msg;
				this.isShowGoodsType = 0;
			},
			//显示全部分类
			showType: function() {
				if (this.isShowGoodsType == 0) {
					this.isShowGoodsType = 1;
				} else {
					this.isShowGoodsType = 0;
				}

			},
			upper: function(e) {
				console.log(e)
			},
			lower: function(e) {
				console.log(e)
			},
			tel: function() {
				// this.current = -1;
				// 调起条码扫描
				var _this = this;

				uni.scanCode({
					scanType: 'barCode',
					success: function(res) {
						console.log(res.result);
						_this.GetBarcodeGoodsMessageModel(res.result);
					},
					fail(e) {
						console.log(e);
					}
				});
			},
			//通过条码扫描商品
			GetBarcodeGoodsMessageModel(barcode) {
				console.log("barcode", barcode);
				let data = {
					BillName: "采购入库单",
					BarCode: barcode,
					Model: "="
				};
				var that = this;
				that.GoodList = [];
				that.checkGoodList = [];
				var tempcheckGoodList = [];
				Apis.GetBarcodeGoodsMessageModel(data, function(res) {
					if(res.state){
						if (res.data.ErrorMsg == "获取登录信息失败") {
						
						} else if (res.data.ErrorMsg != "") {
							console.log(res.data.ErrorMsg);
						
						} else {
							console.log(res);
							var item = {};
							if (res.data.ResultMsg.length == 0) {
								Apis.showTip("查无数据");
							}
						   console.log(res.data.ResultMsg);
							res.data.ResultMsg.forEach((item, index) => {
								item = {
									checked: false
								};
								tempcheckGoodList.push(item);
								res.data.ResultMsg[index].Qty = 1;
								res.data.ResultMsg[index].isSet = false;
								res.data.ResultMsg[index].Discount = 100;
								res.data.ResultMsg[index].Tax = 0;
								res.data.ResultMsg[index].IsGift = 0;
								res.data.ResultMsg[index].Remark = "";
								res.data.ResultMsg[index].GoodsStorm = 0;
								// console.log(that.BillType,res.data.ResultMsg[index].SalePrice);
								if (that.BillType == 4 || that.BillType == 13) {
									res.data.ResultMsg[index].Price = Math.round(res.data.ResultMsg[index].SalePrice * 100) / 100;
								} else {
						
									res.data.ResultMsg[index].Price = Math.round(res.data.ResultMsg[index].InPrice * 100) / 100;
								}
								res.data.ResultMsg[index].barcode = res.data.ResultMsg[index].BarCode;
								res.data.ResultMsg[index].urate = res.data.ResultMsg[index].urate;
								res.data.ResultMsg[index].StoreID = that.StoreID;
								res.data.ResultMsg[index].isChange = false;
								res.data.ResultMsg[index].stockqty = Math.round(res.data.ResultMsg[index].stockqty * 100) / 100;
								// res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
								// res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
								// res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
								res.data.ResultMsg[index].DiscountPrice = that.DiscountPrice(res.data.ResultMsg[index]);
								res.data.ResultMsg[index].Amount = that.Amount(res.data.ResultMsg[index]);
								if (res.data.ResultMsg[index].Goods_Image_Path1 == '') {
									res.data.ResultMsg[index].Goods_Image_Path1 = that.unLoadimg;
								} else {
									res.data.ResultMsg[index].Goods_Image_Path1 = allConst.pictureUrl + res.data.ResultMsg[index].Goods_Image_Path1;
								}
							});
							that.checkGoodList = tempcheckGoodList;
							that.GoodList = res.data.ResultMsg;
							that.inputStr = "";
						}
					}else{
						Apis.showTip("服务器开小差了");
					}				
				})
			},
			scroll: function(e) {
				console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			goTop: function(e) {
				this.scrollTop = this.old.scrollTop
				this.$nextTick(function() {
					this.scrollTop = 0
				});
				uni.showToast({
					icon: "none",
					title: "纵向滚动 scrollTop 值已被修改为 0"
				})
			}
		}
	}
</script>
<style>
	@import url("./goodlist.css");
	@import "../../static/uni.css";
</style>
